import api = require('../api')
import user = require('./user/user.service')
import test1 = require('./test1/test1.service')
import Rx = require('rxjs')
const auth = require('feathers-authentication')

function setupAuth(app: any) {

  //开启jwt验证的service name
  const authServices = [
      'database/buyer',
      'test1'
  ]
  //应用hook到所有service
  Rx.Observable.from(Object.keys(app.services))
      .filter((key: string) => {
          let isAuth = authServices.filter(item => key == item).length > 0
          return isAuth
      })
      .subscribe((key: string) => {
          app.services[key].hooks({
              before: {
                  all: [auth.hooks.authenticate('jwt')],
                  find: [],
                  create: [],
                  get: [],
                  update: [],
                  patch: [],
                  remove: [],
              },
              after: {
                  all: [],
                  find: [],
                  create: [],
                  get: [],
                  update: [],
                  patch: [],
                  remove: [],
              }
          })
      })

}

export = function () {
  const app = this
  
  app.configure(api)
  app.configure(user)
  app.configure(test1)
  setupAuth(app)
}
